//当前用户信息
let current_user ={
    "user_id":0,
    "image_url":"https://bpic.588ku.com/back_pic/05/53/30/405af9ab04baed1.jpg!/fw/320/quality/90/unsharp/true/compress/true",
    "account":0};

(function ($) {
    let ready=function (){
        var storage = window.sessionStorage;
        try {
            let user =JSON.parse(storage["current_user"]);
            if(user != null && user != undefined){
                current_user = user;
                document.getElementById("dd").style.display="none";
                document.getElementById("ddd").style.display="none";
                document.getElementById("myAccount").name=current_user.user_id;
                document.getElementById("im").style.display="inline";
                document.getElementById("current_user_url1").setAttribute("src",current_user.image_url);
                document.getElementById("current_user_url2").setAttribute("src",current_user.image_url);
            }
            // console.log(current_user);
        }catch(err) {
            console.log(current_user);
        }
    };
    ready();

    //注册
    $(document).on('click', '.zhuce', function () {
        var data = $("#addForm").serialize();
        $.ajax({
            url: getRootPath_web()+"/user/addUser",
            data: data,
            type: "GET",
            dataType:"json",
            success: function (result) {
                if (result.success) {
                 alert("注册成功");
                }
                else {
                 alert(result.meg);
                }
            }
        });
    });

    //更新用户
    $(document).on('click', '.updateUser', function () {
        alert("aa");
        var data = $("#addForm2").serialize();

        $.ajax({
            url: getRootPath_web()+"/user/updateUser",
            data: data,
            type: "GET",
            dataType:"json",
            success: function (result) {
                if (result.success) {
                    alert("修改成功");
                }
                else {
                    alert(result.meg);
                }
            }
        });
        return false;
    });

    //显示当前用户的信息
    $(document).on('click', '.myInformation', function () {
        var data=document.getElementById("myAccount").name.toString();

        $.ajax({
            url: getRootPath_web()+"/user/findUser",
            data: "ID="+data,
            type: "Post",
            dataType:"json",
            success: function (result) {
                if (result.success) {
                    document.getElementById("a").value=result.map.user.account;
                    document.getElementById("e").value=result.map.user.email;
                    document.getElementById("p").value=result.map.user.password;
                    document.getElementById("cp").value=result.map.user.password;
                    document.getElementById("ph").value=result.map.user.telephone;
                    document.getElementById(result.map.user.sex).checked=true;


                }
                else {
                    alert(result.meg);
                }
            }
        });
    });

    //登录
    $(document).on('click', '.signUpBtn', function () {
        var data = $("#signUpForm").serializeArray();
        // console.log(data);
        // alert(data);
        var obj = {};
        $.each(data,function(i,v){
            obj[v.name] = v.value;
        });

        $.ajax({
            url: getRootPath_web()+"/user/login",
            data: obj,
            type: "Post",
            // async : false,
            dataType:"json",
            success: function (result) {
                if (result.success) {
                    alert("登陆成功");
                    document.getElementById("dd").style.display="none";
                    document.getElementById("ddd").style.display="none";
                    document.getElementById("myAccount").name=result.map.user.userId;
                    document.getElementById("im").style.display="inline";

                    //登录成功隐藏
                    // $('.mfp-bg').css('display','none');
                    // $("#small-dialog").css('display','none');

                    //保存当前登录用户
                    var user = result.map.user;
                    let current_user ={
                        "user_id":user.userId,
                        "image_url":user.imageUrl,
                        "account":user.account};
                    //更换头像
                    $("#current_user_url1").attr("src",current_user.image_url);
                    $("#current_user_url2").attr("src",current_user.image_url);
                    var storage = window.sessionStorage;
                    storage["current_user"] = JSON.stringify(current_user);
                }
                else {
                    // alert(result.meg);
                    // console.log("失败alter");
                }
            }
        });
        return false;
    });

    //删除用户询问
    $(document).on('click', '.deleteUserBtn', function () {
        var ID = $(this).attr("id");

        $("#deleteUser").empty();

        var deteleUserString='<h1 class=\"text-center\"> 确认删除？</h1>';
        $("#deleteUser").append(deteleUserString);
        var deteleUserString='<input type="hidden" id="99" name="userId" value="'+ID+'">';
        $("#deleteUser").append(deteleUserString);

    });

    //更新用户
    $(document).on('click','.updateUserBtn',function(){
        var ID = $(this).attr("id");
//            alert("updateUser");
        $("#updateUserMsg").empty();
        $.ajax({
            url: getRootPath_web()+"/user/findUser",
            data: "ID=" + ID,
            type: "Post",
            dataType:"json",
            success: function (result) {
                if (result.success) {

                    var user = result.map.user;
                    var updateUserString;
                    updateUserString=' <label>我的账号：</label>';
                    $("#updateUserMsg").append(updateUserString);

                    updateUserString='<input type="text" name="account" readonly="readonly" value="'+user.account+'"><br>';
                    $("#updateUserMsg").append(updateUserString);

                    updateUserString=' <label>电子邮箱：</label>';
                    $("#updateUserMsg").append(updateUserString);
                    updateUserString='<input class="form-control" type="text" name="email" value="'+user.email+'"><br>';
                    $("#updateUserMsg").append(updateUserString);
                    updateUserString=' <label>联系方式：</label>';
                    $("#updateUserMsg").append(updateUserString);
                    updateUserString='<input class="form-control" type="text" name="phone" value="'+user.telephone+'"><br>';
                    $("#updateUserMsg").append(updateUserString);
                    updateUserString=' <label>密码：</label>';
                    $("#updateUserMsg").append(updateUserString);
                    updateUserString='<input class="form-control" type="password" name="password" placeholder="至少6位数字或字母" ><br>';
                    $("#updateUserMsg").append(updateUserString);
                    updateUserString='<label>验证密码：</label>';
                    $("#updateUserMsg").append(updateUserString);
                    updateUserString='<input class="form-control" type="password" name="checkPassword" autofocus="autofocus" placeholder="至少6位字母或数字" ><br>';
                    $("#updateUserMsg").append(updateUserString);
                    updateUserString='\t\t<div class="radio">\n' +
                        '\t\t\t\t\t\t\t\t\t\t\t\t\t<label>\n' +
                        '\t\t\t\t\t\t\t\t\t\t\t\t\t\t<input type="radio" name="optionsRadios" id="sex"\n' +
                        '\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t   value="male" > 男\n' +
                        '\t\t\t\t\t\t\t\t\t\t\t\t\t</label>\n' +
                        '\n' +
                        '\t\t\t\t\t\t\t\t\t\t\t\t\t<label>\n' +
                        '\t\t\t\t\t\t\t\t\t\t\t\t\t\t<input type="radio" name="optionsRadios" id="sex1"\n' +
                        '\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t   value="female">\n' +
                        '\t\t\t\t\t\t\t\t\t\t\t\t\t\t女\n' +
                        '\t\t\t\t\t\t\t\t\t\t\t\t\t</label>\n' +
                        '\t\t\t\t\t\t\t\t\t\t\t\t</div>'
                    $("#updateUserMsg").append(updateUserString);

                }
                else {
                    var estateString=result.msg;
                    $("#findEstates").append(estateString);
                }
            }
        });
    });

    //删除用户
    $(document).on('click', '.deleteUser', function () {
        var ID=document.getElementById("99").value;
        $.ajax({
            url: getRootPath_web()+"/user/deleteUser",
            data: "ID=" + ID,
            type: "Post",
            dataType:"json",
            success: function (result) {
                if (result.success) {

                   alert("删除用户成功");

                }
                else {
                   alert("删除用户失败");
                }
            }
        });
    });

})(jQuery);
